package sfmc.biz.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

import sfmc.biz.service.SfmcEmpStatusService;
import sfmc.biz.vo.Sfmc_TB_EMPSTATUS;
import sfmc.common.util.DateUtil;

@Controller
public class SfmcEmpStatusController {
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	@Autowired 
	private SfmcEmpStatusService sfmcEmpStatusService;
	
	@RequestMapping(value="/empStatus_list.do")
	public ModelAndView empStatusList(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_EMPSTATUS sfmc_TB_EMPSTATUS){
		
		if("".equals(sfmc_TB_EMPSTATUS.getSearchFromDate())){
			sfmc_TB_EMPSTATUS.setSearchFromDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
			sfmc_TB_EMPSTATUS.setSearchToDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
			sfmc_TB_EMPSTATUS.setPageNo(1);
			sfmc_TB_EMPSTATUS.setPageSize(20);
			sfmc_TB_EMPSTATUS.setOffSet(0);
			sfmc_TB_EMPSTATUS.setTotalCnt(0);
		}else{
			sfmc_TB_EMPSTATUS.setOffSet((sfmc_TB_EMPSTATUS.getPageNo()-1)*sfmc_TB_EMPSTATUS.getPageSize());
			sfmc_TB_EMPSTATUS.setTotalCnt(sfmcEmpStatusService.getRecordCount(sfmc_TB_EMPSTATUS));
		}
		
		List<Sfmc_TB_EMPSTATUS> empStatusList = sfmcEmpStatusService.getEmpStatusList(sfmc_TB_EMPSTATUS);
		
		mav.addObject("empStatusList", (new Gson()).toJson(empStatusList));
		mav.addObject("empstatusdata", sfmc_TB_EMPSTATUS);
		mav.setViewName("/empstatus/empstatus_list");
		
		return mav;
	}

	@RequestMapping(value="/empstatus_excel.do")
	public void listsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_EMPSTATUS sfmc_TB_EMPSTATUS) throws Exception{
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_EMPSTATUS> excelList = sfmcEmpStatusService.getEmpStatusList(sfmc_TB_EMPSTATUS, true);
		resp.setHeader("Content-Disposition", "attachment; filename=ConfirmListByEmpName.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			HSSFCell headerC = rowHeader.createCell(2);
			HSSFCell headerD = rowHeader.createCell(3);
			HSSFCell headerE = rowHeader.createCell(4);
			HSSFCell headerF = rowHeader.createCell(5);
			HSSFCell headerG = rowHeader.createCell(6);
			HSSFCell headerH = rowHeader.createCell(7);
			headerA.setCellValue(new HSSFRichTextString("주문날짜"));
			headerB.setCellValue(new HSSFRichTextString("업체명"));
			headerC.setCellValue(new HSSFRichTextString("전화번호"));
			headerD.setCellValue(new HSSFRichTextString("고객코드"));
			headerE.setCellValue(new HSSFRichTextString("채널"));
			headerF.setCellValue(new HSSFRichTextString("파일명"));
			headerG.setCellValue(new HSSFRichTextString("확인날짜"));
			headerH.setCellValue(new HSSFRichTextString("담당자"));
			
			sfmc_TB_EMPSTATUS = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i+1);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			HSSFCell cells3 = rowA.createCell(2);
			HSSFCell cells4 = rowA.createCell(3);
			HSSFCell cells5 = rowA.createCell(4);
			HSSFCell cells6 = rowA.createCell(5);
			HSSFCell cells7 = rowA.createCell(6);
			HSSFCell cells8 = rowA.createCell(7);
			cells1.setCellValue(sfmc_TB_EMPSTATUS.getOrdDate());
			cells2.setCellValue(sfmc_TB_EMPSTATUS.getCustName());
			cells3.setCellValue(sfmc_TB_EMPSTATUS.getCustPhone());
			cells4.setCellValue(sfmc_TB_EMPSTATUS.getCustCode());
			cells5.setCellValue(sfmc_TB_EMPSTATUS.getChannelNum());
			cells6.setCellValue(sfmc_TB_EMPSTATUS.getOrdFileName());
			cells7.setCellValue(sfmc_TB_EMPSTATUS.getConfirmDate());
			cells8.setCellValue(sfmc_TB_EMPSTATUS.getEmpName());
		}

		try{
			workbook.write(ost);
			ost.flush();
			fos = new FileOutputStream(new File("C:/ConfirmListByEmpName.xls"));
			workbook.write(fos);
		}catch(Exception e){
			log.error(e);
		}finally{
			if(fos != null){
				try{
					fos.flush();
					fos.close();
				}
				catch(IOException e){
					log.error(e);
				}
			}
		}
	}

}
